package org.csu.webjpetstoreback.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.webjpetstoreback.config.CommonResponse;
//import org.csu.webjpetstoreback.mapper.AccountMapper;
import org.csu.webjpetstoreback.mapper.OrdersMapper;
//import org.csu.webjpetstoreback.mapper.SignonMapper;
//import org.csu.webjpetstoreback.model.Account;
import org.csu.webjpetstoreback.model.Orders;
//import org.csu.webjpetstoreback.model.Signon;
//import org.csu.webjpetstoreback.service.AccountService;
import org.csu.webjpetstoreback.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public CommonResponse<Orders> GetOrdersByID(int orderId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderId",orderId);
        Orders orders = ordersMapper.selectOne(queryWrapper);
        if (orders != null){
            return CommonResponse.BackForSuccess(orders);
        }else {
            return CommonResponse.BackForError("该订单不存在");
        }
    }

    @Override
    public Orders GetOrdersByOrderId(int orderId){
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderId",orderId);
        Orders orders = ordersMapper.selectOne(queryWrapper);
        if (orders != null){
            return orders;
        }else {
            return null;
        }
    }

    @Override
    public CommonResponse<List<Orders>> GetOrders() {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        if (orders.size() > 0){
            return CommonResponse.BackForSuccess(orders);
        }else{
            return CommonResponse.BackForError("当前不存在任何订单");
        }
    }

    @Override
    public CommonResponse<Orders> PostOrders(Orders orders) {
        int result = ordersMapper.insert(orders);
        System.out.println(result);
        if (result ==1){
            return CommonResponse.BackForSuccess("新增订单成功");
        }else{
            return CommonResponse.BackForError("新增订单失败");
        }
    }

    @Override
    public CommonResponse<Orders> PutOrders(Orders orders) {
        UpdateWrapper<Orders> UpdateWrapper = new UpdateWrapper<>();
        int result = ordersMapper.update(orders,UpdateWrapper);
        if (result ==1){
            return CommonResponse.BackForSuccess("订单修改成功");
        }else{
            return CommonResponse.BackForError("订单修改失败");
        }
    }

    @Override
    public String PutOrdersByOrderId(int orderId){
        UpdateWrapper<Orders> UpdateWrapper = new UpdateWrapper<>();
        Orders orders = GetOrdersByOrderId(orderId);
        UpdateWrapper.eq("orderid",orderId);
        UpdateWrapper.set("state","已完成");
        int result = ordersMapper.update(orders,UpdateWrapper);
        if (result ==1){
            return "订单修改成功";
        }else{
            return "订单修改失败";
        }
    }

    @Override
    public CommonResponse<Orders> DeleteOrders(int orderId) {
        QueryWrapper<Orders> QueryWrapper = new QueryWrapper<>();
        int result = ordersMapper.deleteById(orderId);
        if (result ==1){
            return CommonResponse.BackForSuccess("删除成功!!!");
        }else{
            return CommonResponse.BackForError("删除失败!!!");
        }
    }

    @Override
    public CommonResponse<List<Orders>> GetOrdersByUserName(String username){
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",username);
        List<Orders> ordersList = ordersMapper.selectList(queryWrapper);
        if (ordersList != null){
            return CommonResponse.BackForSuccess(ordersList);
        }else {
            return CommonResponse.BackForError("该订单数组不存在");
        }
    }
}
